﻿using System.Collections.Generic;
using Rabbit.Model; 

namespace Rabbit.BusinessLogicLayer
{
    public class SearchBL
    {
        public static int PageSize = 10;
        public static int currentPage = 1;
        private static string currentSearchKeyword;
        public static bool IsLastPage = false;
        private static List<User> SearchUser(string nickName)
        {
            nickName = Utils.UrlEncoder.UrlOAuthEncode(nickName);
            string result = RequestHelper.SendOAuthRequest("/users/search.json", "count=" + PageSize + "&q=" + nickName + "&nick_name=" + nickName + "&page=" + currentPage);
            List<User> tmpUsers = WrappedJsonHelper.Deserialize<List<User>>(result);
            IsLastPage = tmpUsers.Count < PageSize;
            return tmpUsers;
        }

        private static SearchedStatuses SearchStatus(string keyWord)
        {
            keyWord = Utils.UrlEncoder.UrlOAuthEncode(keyWord);
            string result = RequestHelper.SendOAuthRequest("/statuses/search.json", "count=" + PageSize + "&q=" + keyWord + "&page=" + currentPage);
            SearchedStatuses tmpStatuses = WrappedJsonHelper.Deserialize<SearchedStatuses>(result);
            IsLastPage = tmpStatuses.CurrentTotalCount / PageSize <= currentPage;
            return tmpStatuses;
        }

        public static SearchedStatuses GetFirstPageSearchedStatus(string keyWord)
        {
            currentPage = 1;
            currentSearchKeyword = keyWord;
            return SearchStatus(currentSearchKeyword);
        }

        public static SearchedStatuses GetNextPageSearchedStatus()
        {
            currentPage++;
            return SearchStatus(currentSearchKeyword);
        }
        public static SearchedStatuses GetPreviousePageSearchedStatus()
        {
            currentPage--;
            return  SearchStatus(currentSearchKeyword);
        }

        public static List<User> GetFirstPageUser(string keyWord)
        {
            currentPage = 1;
            currentSearchKeyword = keyWord;
            return SearchUser(currentSearchKeyword);
        }

        public static List<User> GetNextPageSearchedUser()
        {
            currentPage++;
            return SearchUser(currentSearchKeyword);
        }

        public static List<User> GetPreviousePageSearchedUser()
        {
            currentPage--;
            return SearchUser(currentSearchKeyword);
        }
    }
}
